/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.manager.impl;

import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.basics.exception.technical.TechnicalIllegalArgumentException;
import com.genia.toolbox.web.context.WebContext;

/**
 * implementation of <code>UrlManager</code> that allow to generate absolute
 * url.
 */
public class AbsoluteUrlManager
    extends AbstractSimpleUrlManager
{

  /**
   * the <code>WebContext</code> of the request.
   */
  private WebContext webContext;



  /**
   * generate an absolute url by adding the contextPath to the url given in
   * parameter.
   * 
   * @param object
   *          the url to transform. It must be a non-empty String
   * @return the url associated to the argument
   * @throws TechnicalException
   *           if the object given in parameter is not correct.
   */
  @Override
  public String getUrl(final Object object)
      throws TechnicalException
  {
    if (object instanceof String) {
      String url = (String) object;
      if (!StringUtils.hasText(url)) {
        throw new TechnicalIllegalArgumentException("url should not be empty", url);
      }
      url = StringUtils.trimWhitespace(url);
      if (!checkUrl(url)) {
        return null;
      }
      if (url.startsWith("/")) {
        url = getContext().getRequest().getContextPath() + url;
      }
      return url;
    }
    throw new TechnicalIllegalArgumentException("object should be a String", object);
  }



  /**
   * check if the given url is correct and must be displayed.
   * 
   * @param url
   *          the url to check
   * @return <code>true</code> if and only if the url is correct and must be
   *         displayed
   * @throws TechnicalException
   *           if an error occured
   */
  protected boolean checkUrl(String url)
      throws TechnicalException
  {
    return true;
  }



  /**
   * getter for the webContext property.
   * 
   * @return the webContext
   */
  public WebContext getContext()
  {
    return webContext;
  }



  /**
   * setter for the webContext property.
   * 
   * @param webContext
   *          the webContext to set
   */
  public void setContext(final WebContext webContext)
  {
    this.webContext = webContext;
  }

}
